package com.example.administrator.u17.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.CartanAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.model.CartanList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class CartanActivity extends AppCompatActivity implements AbsListView.OnScrollListener {

    private ListView lv;
    private ImageView iv;
    private List<CartanList> list;
    private CartanAdapter adapter;
    private int pages=1;
    private boolean isLast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_cartan);
        initView();
        initData();
        setListener();
    }

    private void setListener() {
        lv.setAdapter(adapter);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv.setOnScrollListener(this);
    }

    private void initData() {
        list=new ArrayList<>();
        adapter=new CartanAdapter(this,list);
        loadData(pages);
    }

    private void initView() {
        lv= (ListView) findViewById(R.id.lv_activity_cartan);
        iv=(ImageView)findViewById(R.id.iv_activity_cartan_back);
    }
    private void loadData(int page){
        RequestParams params=new RequestParams(NetConfig.CARTAN_PATH+page);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Gson g=new Gson();
                List<CartanList> lists=g.fromJson(s,new TypeToken<List<CartanList>>(){}.getType());
                list.addAll(lists);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Toast.makeText(CartanActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (isLast && scrollState==SCROLL_STATE_IDLE){
            loadData(pages++);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        isLast=(firstVisibleItem+visibleItemCount)==totalItemCount;
    }
}
